package dao.impl;

import bean.VideoHall;
import dao.VideoHallDao;
import util.DBCon;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class VideoHallDaoImpl implements VideoHallDao {
    public List<VideoHall> getAllVideoHall() {
        Connection conn = DBCon.getConnection();
        if(conn == null){
            System.out.println("数据库连接失败");
        }
        try {
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("select * from table_video_hall");
            List<VideoHall> videoHallList = new ArrayList<>();
            while (rs.next()){
                VideoHall videoHall = new VideoHall();
                videoHall.setName(rs.getString(1));
                videoHallList.add(videoHall);
            }
            return videoHallList;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    @Override
    public List<VideoHall> getVideoHallByCinemaId(int cinemaId) {
        Connection conn = DBCon.getConnection();
        if(conn == null){
            System.out.println("数据库连接失败");
        }
        try {
            PreparedStatement pStmt = conn.prepareStatement("select * from table_video_hall where cinema_id = ?");
            pStmt.setInt(1,cinemaId);
            List<VideoHall> videoHallList = new ArrayList<>();
            ResultSet rs =  pStmt.executeQuery();
            while (rs.next()){
               VideoHall videoHall =  new VideoHall();
               videoHall.setCinemaId(cinemaId);
               videoHall.setId(rs.getInt("video_hall_id"));
               videoHall.setSeatNum(rs.getInt("seat_num"));
videoHall.setRow(rs.getInt("row"));
videoHall.setColumn(rs.getInt("column"));
               videoHall.setName(rs.getString("name"));
               videoHallList.add(videoHall);
            }
            return videoHallList;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public VideoHall getVideoById(int id) {
        Connection conn = DBCon.getConnection();
        if(conn == null){
            System.out.println("数据库连接失败");
        }
        try {
            PreparedStatement pStmt = conn.prepareStatement("select * from table_video_hall where video_hall_id = ?");
            pStmt.setInt(1,id);
            ResultSet rs =  pStmt.executeQuery();
            if(rs.next()){
                VideoHall videoHall =  new VideoHall();
                videoHall.setId(id);
                videoHall.setCinemaId(rs.getInt("cinema_id"));
                videoHall.setSeatNum(rs.getInt("seat_num"));
videoHall.setRow(rs.getInt("row"));
videoHall.setColumn(rs.getInt("column"));
                videoHall.setName(rs.getString("name"));
                return videoHall;
            }
  return null;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
